var is = require('is');

// 域名组成
var charArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var numberArray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var symbolArray = ['-'];
var mixArray = [];
// 域名长度限制
var MAX_LENGTH = 128;
var fixedArray = ['cloud', 'data', 'game', 'edu', 'health', 'medical', 'media', 'smart', 'home', 'yun', 'bigdata'];
var domainExArray = ['com', 'net'];
mixArray = mixArray.concat(charArray, numberArray, symbolArray);

/**
 * generate domain
 * @int length
 * @string fixed string
 * @int length
 */
var generateDomain = function (preLen, fixed, sufLen) {
	var result = [];
	var preArray = [];
	var sufArray = [];
	var tmp = [];
	var tmptmp = [];
	if (!is.int(preLen)) {
		preLen = 0;
	}
	if (!is.int(sufLen)) {
		sufLen = 0;
	}
	if (!is.string(fixed)) {
		fixed = '';
	}
	// 生成前缀数组
	tmp = mixArray;
	for (var i = 0; i < preLen; i++) {
		for (var prepre in mixArray) {
			for (var pre in preArray) {
				tmp.push(mixArray[prepre] + preArray[pre]);
			}
		}
		preArray = tmp;
		tmp = [];
	}
	// 生成后缀数组
	tmp = mixArray;
	for (var i = 0; i < sufLen; i++) {
		for (var sufsuf in mixArray) {
			for (var suf in sufArray) {
				tmp.push(mixArray[sufsuf] + sufArray[suf]);
			}
		}
		sufArray = tmp;
		tmp = [];
	}
	// 组合前缀和固定部分
	tmp = [];
	for (var pre in preArray) {
		tmp.push(preArray[pre] + fixed);
	}
	if (preArray.length == 0) {
		tmp.push(fixed);
	}
	// 组合临时和后缀
	for (var suf in sufArray) {
		for (var dom in tmp) {
			tmptmp.push(tmp[dom] + sufArray[suf]);
		}
	}
	if (sufArray.length == 0) {
		tmptmp = tmp;
	}
	// 组合临时临时和扩展
	for (var ex in domainExArray) {
		for (var dom in tmptmp) {
			result.push(tmptmp[dom] + '.' + domainExArray[ex]);
		}
	}
	return result;
}

console.log(generateDomain(0, 'vvv', 0));
console.log(generateDomain(0, 'vvv', 3));
console.log(generateDomain(3, 'vvv', 0));
console.log(generateDomain(3, 'vvv', 3));